package cn.com.generaldata.os.service.indicator_evaluation.industry_structure_analysis;

import org.apache.commons.lang3.Validate;

import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.IndicatorEvaluationCalculateService;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

/**
 * 同比第二产业生产率上升率评估指标计算类.
 */
public class SecondProductivityRiseRateLastCalculateService extends IndicatorEvaluationCalculateService {

	private String FirstAddName = ProvinceIndicatorConstant.SECOND_INDICATOR_NAME;
	private String FirstEnployedName = ProvinceIndicatorConstant.SECOND_EMPLOYED_INDICATOR_NAME;

	@Override
	protected Double calculate(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		// 同比年份
		int fixBaseYear = indicatorAccountVO.getYear() - 1;
		Double firstAddValue = getBaseIndicatorVal(indicatorAccountVO, indicatorAccountVO.getYear(), FirstAddName);
		Double firstEnployed = getBaseIndicatorVal(indicatorAccountVO, indicatorAccountVO.getYear(), FirstEnployedName);
		Double firstAddValueBase = getBaseIndicatorVal(indicatorAccountVO, fixBaseYear, FirstAddName);
		Double firstEnployedBase = getBaseIndicatorVal(indicatorAccountVO, fixBaseYear, FirstEnployedName);

		Validate.isTrue(!(Double.valueOf(0).equals(firstEnployed)));
		Validate.isTrue(!(Double.valueOf(0).equals(firstEnployedBase)));
		Double firstVt = firstEnployed == 0 ? 0 : firstAddValue / firstEnployed;
		Double firstV0 = firstEnployedBase == 0 ? 0 : firstAddValueBase / firstEnployedBase;

		Validate.isTrue(!(Double.valueOf(0).equals(firstV0)));
		return firstV0 == 0 ? 0 : firstVt / firstV0;

	}

	private Double getBaseIndicatorVal(IndicatorAccountVO indicatorAccountVO, int year, String name) {
		Double result = getInputOrAccountingIndicatorValue(indicatorAccountVO, year, name);
		return result == null ? 0d : result;
	}

}
